<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="billcode">
        <el-input
          v-model="queryParams.billcode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单日期" prop="billdate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.billdate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择订单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户" prop="gid">
        <el-select v-model="queryParams.gid" filterable placeholder="请选择客户" clearable>
          <el-option
            v-for="guest in guestList"
            :key="guest.gid"
            :label="guest.name"
            :value="guest.gid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开单人" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入开单人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值类型" prop="billtype">
        <el-select v-model="queryParams.billtype" placeholder="请选择充值类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.bill_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="billstate">
        <el-select v-model="queryParams.billstate" placeholder="请选择单据状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.bill_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:recharge:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:recharge:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:recharge:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:recharge:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rechargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="订单号" prop="billcode" />
      <el-table-column label="订单日期" prop="billdate" width="180"/>
      <el-table-column label="客户" prop="gid" >
        <template slot-scope="scope">
          <span>{{ guesMatching(scope.row.gid) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值金额" prop="addvalue" />
      <el-table-column label="充值后余额" prop="amount" />
      <el-table-column label="收款账户" prop="accid" >
        <template slot-scope="scope">
          <span>{{ accountMatching(scope.row.accid) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开单人" prop="userid" >
        <template slot-scope="scope">
          <span>{{ employeeMatching(scope.row.userid) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值类型" prop="billtype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_type" :value="scope.row.billtype"/>
        </template>
      </el-table-column>
      <el-table-column label="单据状态" prop="billstate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_state" :value="scope.row.billstate"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >查看</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单号" prop="billcode">
              <el-input v-model="form.billcode" placeholder="请输入订单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户" prop="gid">
              <el-select v-model="form.gid" filterable placeholder="请选择客户" clearable>
                <el-option
                  v-for="guest in guestList"
                  :key="guest.gid"
                  :label="guest.name"
                  :value="guest.gid"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="充值金额" prop="addvalue">
              <el-input v-model="form.addvalue" placeholder="请输入充值金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值后余额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入充值后余额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收款账户" prop="accid">
              <el-select v-model="form.accid" filterable placeholder="请选择开单人" clearable>
                <el-option
                  v-for="account in accountList"
                  :key="account.accid"
                  :label="account.name+' - '+account.cardnumber"
                  :value="account.accid"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开单人" prop="userid">
              <el-select v-model="form.userid" filterable placeholder="请选择开单人" clearable>
                <el-option
                  v-for="employee in employeeList"
                  :key="employee.empid"
                  :label="employee.name"
                  :value="employee.empid"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="充值类型" prop="billtype">
              <el-select v-model="form.billtype" placeholder="请选择充值类型">
                <el-option
                  v-for="dict in dict.type.bill_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单据状态" prop="billstate">
              <el-select v-model="form.billstate" placeholder="请选择单据状态">
                <el-option
                  v-for="dict in dict.type.bill_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>



        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="form.rechargeid==null" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecharge, getRecharge, delRecharge, addRecharge, updateRecharge,getCode } from "@/api/system/recharge";
import {listGuestAll} from "@/api/system/guest";
import {listEmployeeAll} from "@/api/system/employee";
import {getAccountAll} from "@/api/system/account";

export default {
  name: "Recharge",
  dicts: ['bill_state', 'bill_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值订单表格数据
      rechargeList: [],
      //客户列表
      guestList: [],
      //账号列表
      accountList:[],
      //业务员
      employeeList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rechargeid: null,
        billcode: null,
        billdate: null,
        gid: null,
        addvalue: null,
        amount: null,
        accid: null,
        userid: null,
        billtype: null,
        billstate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getGuesList();
    this.getEmployeeList();
    this.getAccountList();
  },
  methods: {
    getAccountList(){
      getAccountAll().then(response => {
        this.accountList = response.data;
      })
    },
    accountMatching(accid) {
      let name = "-";
      this.accountList.map(item => {
        if (accid == item.accid) {
          name = item.name+" - "+item.cardnumber;
          return
        }
      })
      return name;
    },
    getEmployeeList(){
      listEmployeeAll().then(response => {
        this.employeeList = response.data;
      })
    },
    employeeMatching(userid) {
      let name = "-";
      this.employeeList.map(item => {
        if (userid == item.empid) {
          name = item.name;
          return
        }
      })
      return name;
    },
    getGuesList() {
      listGuestAll().then(response => {
        this.guestList = response.data;
      })
    },
    guesMatching(gid) {
      let name = "-";
      this.guestList.map(item => {
        if (gid == item.gid) {
          name = item.name;
          return
        }
      })
      return name;
    },
    getCode(){
      getCode().then(response => {
        this.form.billcode= response.data;
      })
    },
    /** 查询充值订单列表 */
    getList() {
      this.loading = true;
      listRecharge(this.queryParams).then(response => {
        this.rechargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rechargeid: null,
        billcode: null,
        billdate: null,
        gid: null,
        addvalue: null,
        amount: null,
        accid: null,
        userid: null,
        billtype: null,
        billstate: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rechargeid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值订单";
      this.getCode()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rechargeid = row.rechargeid || this.ids
      getRecharge(rechargeid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rechargeid != null) {
            // updateRecharge(this.form).then(response => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.getList();
            // });
          } else {
            addRecharge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rechargeids = row.rechargeid || this.ids;
      this.$modal.confirm('是否确认删除充值订单编号为"' + rechargeids + '"的数据项？').then(function() {
        return delRecharge(rechargeids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/recharge/export', {
        ...this.queryParams
      }, `recharge_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
